#include "complexity.h"
#include "connectedComponents.h"

bool complexCompare(Mat a, Mat b) {
    return (complexity(a) > complexity(b));
}

double complexity2(Mat cur) {
    Mat outline;
    Mat temp;
    resize(cur, temp, Size(0,0), 3, 3, INTER_NEAREST);
    int ink = countNonZero(temp); // get number of 1 pixels
    dilate(temp, outline, Mat());
    outline = outline - temp; // get outline of character
    dilate(outline, outline, Mat());
    int perimeter = countNonZero(outline) / 3;

    return 1.0 / ((float)perimeter * perimeter / ink);
}

int symmetry(Mat cur) {
    Mat flipped, sub;
    flip(cur, flipped, -1);
    
    sub = flipped - cur;
    return countNonZero(sub);
}

double complexity(Mat cur) {
//    Moments m = moments(cur, 1);
//    double hu[7];
//    HuMoments(m, &hu[0]);
//    float error = ((hu[0] - 0.35) * (hu[0] - 0.35));
//    error += ((hu[1] - 0.0053) * (hu[1] - 0.0053));
//    error += ((hu[2] - 0.0016) * (hu[2] - 0.0016));
//    error += ((hu[3] - 0.00022) * (hu[3] - 0.00022));
//    error += ((hu[4] - 0.00022) * (hu[4] - 0.00022));
//    return (1.0 / complexity2(cur)) + (1.0 / error);
    //////
//    Mat disp;//(100, 100, CV_32FC1);
//    resize(cur, disp, Size(0,0), 10, 10, INTER_NEAREST);
    int c = connectedComponents(&cur);
    int h = holes(&cur);
//    int h = holes(disp);
    if (c == 1 && h == 0)
        return 1000;
    else
        return 1.0 / ((c-1)*(c-1) + h*h);
//    return 1.0 / complexity2(cur);
}

